//    Copyright (C) Kherty.  All rights reserved.
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public class DateAxis : Axis
    {
        private const int defaultLabelOffset = 100;
        private const string name = "dateAx";

        private bool _auto;
        private TimeUnit _baseTimeUnit;

        private int _lblOffset = defaultLabelOffset;
        private TimeUnit _majorTimeUnit;
        private double? _majorUnit;
        private TimeUnit _minorTimeUnit;
        private double? _minorUnit;

        public bool Auto
        {
            get { return _auto; }
            set { _auto = value; notifyPropertyChanged("Auto"); }
        }

        public int LabelOffset
        {
            get { return _lblOffset; }
            set { _lblOffset = value; notifyPropertyChanged("LabelOffset"); }
        }

        public TimeUnit BaseTimeUnit
        {
            get { return _baseTimeUnit; }
            set { _baseTimeUnit = value; notifyPropertyChanged("BaseTimeUnit"); }
        }

        public TimeUnit MajorTimeUnit
        {
            get { return _majorTimeUnit; }
            set { _majorTimeUnit = value; notifyPropertyChanged("MajorTimeUnit"); }
        }

        public double? MajorUnit
        {
            get { return _majorUnit; }
            set { _majorUnit = value; notifyPropertyChanged("MajorUnit"); }
        }

        public TimeUnit MinorTimeUnit
        {
            get { return _minorTimeUnit; }
            set { _minorTimeUnit = value; notifyPropertyChanged("MinorTimeUnit"); }
        }

        public double? MinorUnit
        {
            get { return _minorUnit; }
            set { _minorUnit = value; notifyPropertyChanged("MinorUnit");}
        }

        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read(name))
            {
                ReadXmlBase(c);
                Auto = c.ReadOptionalElementBooleanAttribute("auto", false);
                LabelOffset = c.ReadOptionalElementIntegerAttribute("lblOffset", 100);
                BaseTimeUnit = c.ReadOptionalElementEnumAttribute("baseTimeUnit", TimeUnit.Days);
                MajorTimeUnit = c.ReadOptionalElementEnumAttribute("majorTimeUnit", TimeUnit.Days);
                MajorUnit = c.ReadOptionalElementDoubleAttribute("majorUnit");
                //\\hack even though it does not match the doc
                if (c.ReaderLocalName == "majorTimeUnit")
                    MajorTimeUnit = c.ReadOptionalElementEnumAttribute("majorTimeUnit", TimeUnit.Days);
                MinorTimeUnit = c.ReadOptionalElementEnumAttribute("minorTimeUnit", TimeUnit.Days);
                MinorUnit = c.ReadOptionalElementDoubleAttribute("minorUnit");
                if (c.ReaderLocalName == "minorTimeUnit")
                    MinorTimeUnit = c.ReadOptionalElementEnumAttribute("minorTimeUnit", TimeUnit.Days);
                Extension = c.ReadOptionalElement<ExtensionElement>("extLst");
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write(name))
            {
                WriteXmlBase(c);
                c.WriteOptionalElementBooleanAttribute("auto", Auto);
                c.WriteOptionalElementIntegerAttribute("lblOffset", LabelOffset, 100);
                c.WriteOptionalElementEnumAttribute("baseTimeUnit", BaseTimeUnit, TimeUnit.Days);
                c.WriteOptionalElementDoubleAttribute("majorUnit", MajorUnit);
                c.WriteOptionalElementEnumAttribute("majorTimeUnit", MajorTimeUnit, TimeUnit.Days);
                c.WriteOptionalElementDoubleAttribute("minorUnit", MinorUnit);
                c.WriteOptionalElementEnumAttribute("minorTimeUnit", MinorTimeUnit, TimeUnit.Days);
                c.WriteOptionalElement(Extension);
            }
        }
    }
}
